/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

/**
 *
 */
package com.wallellen.wechat.token;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.wallellen.core.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 * @date 2015年1月29日
 */
public abstract class Token {
    private static Logger logger = LoggerFactory.getLogger(Token.class);

    private String token;   //token
    private long expires;         //token有效时间

    private long tokenTime;       //token产生时间
    private int redundance = 10 * 1000;  //冗余时间，提前10秒就去请求新的token

    /**
     * 得到access token
     */
    public String getToken() {
        return this.token;
    }

    /**
     * 得到有效时间
     */
    public long getExpires() {
        return expires;
    }

    /**
     * 请求信的access token
     * http请求方式: GET
     * https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
     * {"access_token":"ACCESS_TOKEN","expires_in":7200}
     * {"errcode":40013,"errmsg":"invalid appid"}
     */
    public boolean request() {
        String url = accessTokenUrl();
        String result = HttpUtils.get(url);
        if (Strings.isNullOrEmpty(result))
            return false;
        if (!parseData(result)) {
            return false;
        }
        logger.info("token获取成功");
        return true;
    }

    /**
     * 解析token数据
     *
     * @param data
     * @return
     */
    private boolean parseData(String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String tokenName = tokenName();
        String expiresInName = expiresInName();
        try {
            String token = jsonObject.get(tokenName).toString();
            if (Strings.isNullOrEmpty(token)) {
                logger.error("token获取失败,获取结果" + data);
                return false;
            }
            this.token = token;
            this.tokenTime = (new Date()).getTime();
            String expiresIn = jsonObject.get(expiresInName).toString();
            if (Strings.isNullOrEmpty(expiresIn)) {
                logger.error("token获取失败,获取结果" + expiresIn);
                return false;
            } else {
                this.expires = Long.valueOf(expiresIn);
            }
        } catch (Exception e) {
            logger.error("token 结果解析失败，token参数名称: " + tokenName
                    + "有效期参数名称:" + expiresInName
                    + "token请求结果:" + data);
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * token的参数名称
     *
     * @return
     */
    protected abstract String tokenName();

    /**
     * expireIn的参数名称
     *
     * @return
     */
    protected abstract String expiresInName();

    /**
     * 组织accesstoken的请求utl
     *
     * @return
     */
    protected abstract String accessTokenUrl();

    /**
     * accessToken 是否有效
     *
     * @return true:有效，false: 无效
     */
    public boolean isValid() {
        //黑名单判定法
        if (Strings.isNullOrEmpty(this.token))
            return false;
        if (this.expires <= 0)
            return false;
        //过期
        if (isExpire())
            return false;
        return true;
    }

    /**
     * 是否过期
     *
     * @return true 过期 false：有效
     */
    private boolean isExpire() {
        Date currentDate = new Date();
        long currentTime = currentDate.getTime();
        long expiresTime = expires * 1000 - redundance;
        //判断是否过期
        if ((tokenTime + expiresTime) > currentTime)
            return false;
        return true;
    }
}
